package csu.web.jpetstore.web.servlet;

import csu.web.jpetstore.domain.Account;
import csu.web.jpetstore.domain.Product;
import csu.web.jpetstore.persistence.impl.AccountDaoImpl;
import csu.web.jpetstore.service.AccountService;
import csu.web.jpetstore.service.CatalogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

public class EditAccountServlet extends HttpServlet {
    private static final String MYACCOUNT_VIEW = "/WEB-INF/jsp/account/editAccount.jsp";
    private static final String MAIN_VIEW = "/WEB-INF/jsp/catalog/main.jsp";

    private String RegMsg;
    private String username;
    private String userid;
    private String password;
    private String confirm;
    private String firstname;
    private String lastname;
    private String address1;
    private String address2;
    private String city;
    private String status;
    private String state;
    private String email;
    private String phone;
    private String zip;
    private String country;
    private String language;
    private String favcategory;
    private boolean enablemylist;
    private boolean enablebanner;

    private AccountService accountService;
    private HttpSession session;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        session=req.getSession();
        accountService = new AccountService();

        this.username=req.getParameter("USERNAME");
        this.password=req.getParameter("PASSWORD");
        this.confirm=req.getParameter("CONFIRM");
        this.firstname=req.getParameter("FIRSTNAME");
        this.lastname=req.getParameter("LASTNAME");
        this.email=req.getParameter("EMAIL");
        this.phone=req.getParameter("PHONE");
        this.address1=req.getParameter("ADDRESS1");
        this.address2=req.getParameter("ADDRESS2");
        this.city=req.getParameter("CITY");
        this.state=req.getParameter("STATE");
        this.zip=req.getParameter("COUNTRY");
        this.language=req.getParameter("LANGUAGE");
        this.favcategory=req.getParameter("PET");
        this.country=req.getParameter("COUNTRY");
        this.enablemylist= Boolean.parseBoolean(req.getParameter("LISTOPTION"));
        this.enablebanner= Boolean.parseBoolean(req.getParameter("BANNEROPTION"));

        if(!validate()){
            req.setAttribute("Msg",RegMsg);
            req.getRequestDispatcher(MYACCOUNT_VIEW).forward(req,resp);
        }
        else{
            Account result=accountService.getAccount(this.username);
            result.setCountry(this.country);
            result.setEmail(this.email);
            result.setCity(this.city);
            result.setStatus(this.status);
            result.setState(this.state);
            result.setZip(this.zip);
            result.setPhone(this.phone);
            result.setEmail(this.email);

            result.setPassword(this.password);

            result.setLastName(this.lastname);
            result.setFirstName(this.firstname);
            result.setAddress1(this.address1);
            result.setAddress2(this.address2);
            result.setFavouriteCategoryId(this.favcategory);
            result.setBannerOption(this.enablebanner);
            result.setListOption(this.enablemylist);
            result.setLanguagePreference(this.language);
            try {
                accountService.updateAccount(result);
                result.setPassword(null);
                session.setAttribute("loginAccount", result);

                if(result.isListOption()){
                    CatalogService catalogService = new CatalogService();
                    List<Product> myList = catalogService.getProductListByCategory(result.getFavouriteCategoryId());
                    session.setAttribute("myList", myList);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            req.getRequestDispatcher(MYACCOUNT_VIEW).forward(req,resp);
        }
    }


    private boolean validate(){
        if(!this.password.equals(this.confirm)){
            RegMsg="密码输入不一致！";
            return false;
        }
        else if(username==null||username.equals("")){
            RegMsg = "个人信息不完整！";
            return false;
        }
        else if(address1==null||address1.equals("")){
            if(address2==null||address2.equals("")) {
                RegMsg = "个人信息不完整！";
                return false;
            }
        }
        else if (phone==null||email==null||phone.equals("")||email.equals("")){
            RegMsg = "个人信息不完整！";
            return false;
        }
        return true;
    }
}
